<?php

// @formatter:off
/**
 * @file WCActiveTag.php
 * @author Alejandro Dario Simi
 * @date $Date: 2014-05-04 01:33:52 +0000 (Sun, 04 May 2014) $
 *
 * $Id: WCActiveTag.php 111 2014-05-04 01:33:52Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/trunk/includes/representations/WCActiveTag.php $
 */
// @formatter:on

/**
 * @class WCActiveTags
 */
class WCActiveTag {
	protected $_name = false;
	protected $_tag = false;
	protected $_ok = false;
	/**
	 * @var WCActiveTagHook
	 */
	protected $_hook = false;
	/**
	 * Class constructor.
	 */
	public function __construct() {
		
	}
	//
	// Public methods.
	/**
	 * @return WCActiveTagHook
	 */
	public function hook() {
		return $this->_hook;
	}
	public function load($name, WCGroup &$group) {
		//
		// Setting default value to be returned.
		$loaded = false;
		//
		// At this point, this object is not okey.
		$this->_ok = false;
		//
		// Loading global pointers;
		global $wcActiveTagHooks;
		global $wcSpecialTags;
		//
		// Checking if $name is an active tag.
		if(isset($wcActiveTagHooks[$name])) {
			//
			// Saving name.
			$this->_name = $name;
			//
			// Checking hook structure.
			$this->_ok = isset($wcActiveTagHooks[$name]["class"]) && isset($wcActiveTagHooks[$name]["description"]);
			//
			// Attempting to load tag
			if($this->_ok) {
				//
				// Loading tag object.
				$this->_tag = WCTagsHolder::I()->itemByNameInGroup($name, $group);
				$this->_tag = array_shift($this->_tag);
				//
				// Checking tag.
				if(!$this->_tag || !$this->_tag->ok()) {
					$this->_tag = false;
					$this->_ok = false;
				}
			}
			//
			// Attempting to create a hook manager.
			if($this->_ok) {
				//
				// Checking if a file should be loaded to obtain
				// the hook manager.
				if(isset($wcActiveTagHooks[$name]["path"])) {
					if(is_file($wcActiveTagHooks[$name]["path"])) {
						require_once WCSanitizer::SystemPath($wcActiveTagHooks[$name]["path"]);
					} else {
						$this->_ok = false;
					}
				}
				//
				// Checking class existence before using it.
				if($this->_ok && class_exists($wcActiveTagHooks[$name]["class"])) {
					//
					// Creating the appropriate manager.
					$this->_hook = new $wcActiveTagHooks[$name]["class"]();
					$this->_hook->setTag($this->_tag);
				} else {
					$this->_ok = false;
				}
			}
			if($this->_ok) {
				//
				// At this point everything seems okey.
				$loaded = true;
			}
		} elseif(in_array($name, $wcSpecialTags)) {
			//
			// It seems this is a simple special tag.
			//
			// Saving name.
			$this->_name = $name;
			//
			// Loading tag object.
			$this->_tag = WCTagsHolder::I()->itemByNameInGroup($name, $group);
			$this->_tag = array_shift($this->_tag);
			//
			// Checking tag.
			if($this->_tag && $this->_tag->ok()) {
				//
				// At this point everything seems okey.
				$loaded = true;
				//
				// Also, this object is considered to be okey.
				$this->_ok = true;
			} else {
				$this->_tag = false;
			}
		}
		//
		// Returning loading result.
		return $loaded;
	}
	public function name() {
		return $this->_name;
	}
	public function ok() {
		return $this->_ok;
	}
	public function tag() {
		return $this->_tag;
	}
}
